package com.moba.service;

import com.moba.domain.UserMoneyApply;
import com.moba.emums.BiChuOrRu;
import com.moba.emums.BiGetType;
import com.moba.emums.BiType;
import com.moba.mapper.UserMoneyApplyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author li, bai,qinag
 * @version 2019-03-06 11:20
 * @date 2019-03-06 11:20
 */
@Service
@Transactional
public class UserMoneyApplyService {

  @Resource private UserMoneyApplyMapper userMoneyApplyMapper;

  @Resource private UserInfoService userInfoService;

  public UserMoneyApply getEntity(UserMoneyApply t) {
    return this.userMoneyApplyMapper.selectEntity(t);
  }

  public BigDecimal getEntitySum(UserMoneyApply t) {
    return this.userMoneyApplyMapper.selectEntitySum(t);
  }

  public Integer getEntityCount(UserMoneyApply t) {
    return this.userMoneyApplyMapper.selectEntityCount(t);
  }

  public List<UserMoneyApply> getEntityList(UserMoneyApply t) {
    return this.userMoneyApplyMapper.selectEntityList(t);
  }

  public Integer createEntity(UserMoneyApply t) {

    if (null != t.getMap().get("saveUserOrderMoneySum")) {

      Boolean updateBiFlag =
          userInfoService.updateBi(
              t.getAddUid(),
              t.getUserId(),
              BiGetType.BI_GET_TYPE_80.getIndex(),
              t.getOptType(),
              BiChuOrRu.BI_CHU_OR_RU_X1.getIndex(),
              new BigDecimal(t.getMap().get("sub_bi_count").toString()),
              null);

      if (!updateBiFlag) {
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return -1;
      }

      updateBiFlag =
          userInfoService.updateBi(
              t.getAddUid(),
              t.getUserId(),
              BiGetType.BI_GET_TYPE_80.getIndex(),
              BiType.BI_TYPE_400.getIndex(),
              BiChuOrRu.BI_CHU_OR_RU_1.getIndex(),
              t.getCashCount(),
              null);
      if (!updateBiFlag) {
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return -1;
      }
      this.userMoneyApplyMapper.insertEntity(t);
      return t.getId();
    }

    /** 这个用于币种的出入,然后操作的东西 1、需要插入出入记录 2、更新用户对应字段 3、插入提现记录 */
    if (null != t.getMap().get("biOutAndDoThing")) {
      Boolean updateBiFlag =
          userInfoService.updateBi(
              t.getAddUid(),
              t.getUserId(),
              t.getCashType(),
              Integer.valueOf(t.getMap().get("biType").toString()).intValue(),
              BiChuOrRu.BI_CHU_OR_RU_X1.getIndex(),
              t.getCashCount(),
              null);
      if (updateBiFlag) {
        this.userMoneyApplyMapper.insertEntity(t);
        return t.getId();
      } else {
        return 0;
      }
    } else {
      return this.userMoneyApplyMapper.insertEntity(t);
    }
  }

  public int updateEntity(UserMoneyApply t) {
    if (null != t.getMap().get("returnBiUsdt")) {
      // 审核不通过
      Boolean updateBiFlag =
          userInfoService.updateBi(
              t.getUserId(),
              t.getId(),
              BiGetType.BI_GET_TYPE_12.getIndex(),
              BiType.BI_TYPE_300.getIndex(),
              BiChuOrRu.BI_CHU_OR_RU_1.getIndex(),
              t.getCashCount(),
              null);
      if (updateBiFlag) {
        return this.userMoneyApplyMapper.updateEntity(t);
      } else {
        return 0;
      }
    } else {
      return this.userMoneyApplyMapper.updateEntity(t);
    }
  }

  public int removeEntity(UserMoneyApply t) {
    return this.userMoneyApplyMapper.deleteEntity(t);
  }

  public List<UserMoneyApply> getEntityPaginatedList(UserMoneyApply t) {
    return this.userMoneyApplyMapper.selectEntityPaginatedList(t);
  }
}
